package com.tjsinfo.tjsoa.mail.action;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;

import com.tjsinfo.tjsoa.Dao.IJDBCDao;
import com.tjsinfo.tjsoa.common.DealString;
import com.tjsinfo.tjsoa.common.SessionUser;
import com.tjsinfo.tjsoa.common.ToolKit;
import com.tjsinfo.tjsoa.mail.bo.ITjsDraftBo;

import com.tjsinfo.tjsoa.mail.vo.TjsAnnex;
import com.tjsinfo.tjsoa.mail.vo.TjsDraft;

import com.tjsinfo.tjsoa.system.vo.Files;
import com.tjsinfo.tjsoa.system.vo.TSystemUser;

/**
 * 保持草稿
 * @author wangxiaowei
 *
 */
public class SaveDrafAction extends Action{
	private ITjsDraftBo tjsDraftBo;
	public ITjsDraftBo getTjsDraftBo() {
		return tjsDraftBo;
	}

	public void setTjsDraftBo(ITjsDraftBo tjsDraftBo) {
		this.tjsDraftBo = tjsDraftBo;
	}
	private IJDBCDao jdbcdao;
	
	public IJDBCDao getJdbcdao() {
		return jdbcdao;
	}

	public void setJdbcdao(IJDBCDao jdbcdao) {
		this.jdbcdao = jdbcdao;
	}

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			MessageResources message=getResources(request);
			//提交上传附件
			String fromFilePath=message.getMessage(request.getLocale(), "system.uploadFile.tempFilePath");
			String nowSQL = ToolKit.getProperties("com/tjsinfo/tjsoa/mail/properties/systemResource.properties", this).getProperty("nowSQL");
			String toFilePath = ToolKit.getProperties("com/tjsinfo/tjsoa/mail/properties/systemResource.properties", this).getProperty(nowSQL+".fileurl");
			List<Files> fileList=DealString.fileCopyTofile(request, fromFilePath, toFilePath);
			List<TjsAnnex> list = new ArrayList<TjsAnnex>();
			for (Files files : fileList) {
				TjsAnnex tjsAnnex = new TjsAnnex();
				tjsAnnex.setAnnexAddress("");
				tjsAnnex.setAnnexName(files.getNewFileName());
				tjsAnnex.setAnnexZhenName(files.getOldFileName());
				File   f   =   new   File(toFilePath+files.getNewFileName()); 
				int size = ((int)(100*f.length())/1024);
				tjsAnnex.setAnnexSize(size);
				list.add(tjsAnnex);
			}
		
		String value = request.getParameter("contest");
		String title = request.getParameter("title");
		TjsDraft tjsDraft = new TjsDraft();//草稿
		/*tjsDraft.setDraftDatetime(new Date());
		tjsDraft.setDraftStart(2);
		tjsDraft.setDraftTag(0);
		tjsDraft.setDraftTitle(title);
		tjsDraft.setDraftType(2);
		tjsDraft.setDraftValue(value);*/
		TSystemUser	 user = new TSystemUser();
		user.setUserId(SessionUser.getUserId(request));
		String uuid = UUID.randomUUID().toString().replace("-", "");
		String uuuuuuu = request.getParameter("uuuuuuu");
		String sendusers = request.getParameter("sendusers");
		tjsDraft.setTjsUser(user);
		tjsDraftBo.addTjsDraf(title, value, new Date(), 2, SessionUser.getUserId(request),"","","",list);
		String sql = "insert into dbo.t_mail_tjsDraft(Draft_id,Draft_title,Draft_value,Draft_datetime,Draft_type,Draft_userid,Draft_start,Draft_tag,sortid)" +
		" values('"+uuid+"','"+title+"','"+value+"',new Date(),2,'"+sendusers+"',2,0,null);";
		//jdbcdao.executeUpdate(sql);
		request.setAttribute("linkurl", "tjsinfo/tjsoa/mail/emailDraftList.do");
		request.setAttribute("message", "email.message.savedraft");
		return mapping.findForward("success");
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return mapping.findForward("error");
		}
	}
	
}

